package com.yun.datastructure.tree.binarytree;


import java.util.LinkedList;

public class ExpressionTree {

    static class TreeNode{
        public String val;
        public TreeNode left;
        public TreeNode right;

        public TreeNode(String val) {
            this.val = val;
        }

        public TreeNode(TreeNode left, String val, TreeNode right) {
            this.left = left;
            this.val = val;
            this.right = right;
        }

        @Override
        public String toString() {
            return String.valueOf(this.val);
        }
    }

    public TreeNode constructExpressionTree(String[] tokens){
        LinkedList<TreeNode> stack=new LinkedList<>();
        for (String t : tokens) {
            switch (t){
                case "+","-","*","/"->{
                    TreeNode right=stack.pop();
                    TreeNode left=stack.pop();
                    TreeNode parent=new TreeNode(t);
                    parent.left=left;
                    parent.right=right;
                    stack.push(parent);
                }
                default -> {
                    stack.push(new TreeNode(t));
                }
            }
        }
        return stack.peek();
    }
}
